package com.lc.ibps.platform.rest.system;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.base.query.QueryFilter;
import com.lc.ibps.api.base.query.QueryOP;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.base.core.util.BeanUtils;
import com.lc.ibps.base.core.util.string.StringUtil;
import com.lc.ibps.base.framework.page.PageList;
import com.lc.ibps.base.web.context.ContextUtil;
import com.lc.ibps.cloud.entity.APIPageList;
import com.lc.ibps.cloud.entity.APIRequest;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.common.system.persistence.entity.NewsPo;
import com.lc.ibps.common.system.service.INewsRightsService;
import com.lc.ibps.common.system.service.INewsService;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;

@RequestMapping(value = "/api/webapi/newsService")
@Api(value = "/newsService", tags = "系统管理-公告服务")
@RestController
public class NewsController extends BaseApiController {

	private INewsService newsService;

	private INewsRightsService newsRightsService;

	public NewsController() {
		newsService = AppUtil.getBean(INewsService.class);
		newsRightsService = AppUtil.getBean(INewsRightsService.class);
	}

	@ApiOperation(value = "公告列表", notes = "公告列表")
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public APIResult<APIPageList<NewsPo>> newsList(
			@ApiParam(name = "request", value = "传入查询请求json字符串", required = true) 
			@RequestBody(required = true) APIRequest request) {
		APIResult<APIPageList<NewsPo>> result = new APIResult<>();
		try {
			QueryFilter queryFilter = getQuerFilter(request);
			newsService.updateStatus();
			boolean isSuper = ContextUtil.isSuper();
			if (!isSuper) {
				// 无论哪个用户都可以有所属组织或者没有所属组织
				if (BeanUtils.isNotEmpty(ContextUtil.getCurrentOrg())) {
					String orgId = ContextUtil.getCurrentOrg().getId();
					List<String> newsIds = newsRightsService.getNewsIdList(orgId);
					if (BeanUtils.isNotEmpty(newsIds)) {
						queryFilter.addParamsFilter("ID_", newsIds);
					} else {
						queryFilter.addFilter("IS_PUBLIC_", "yes", QueryOP.EQUAL);
					}
				} else {
					queryFilter.addFilter("IS_PUBLIC_", "yes", QueryOP.EQUAL);
				}
			}
			String status = request.getParameter("status");
			if (StringUtil.isNotEmpty(status)) {
				queryFilter.addFilter("STATUS_", status, QueryOP.EQUAL);
			}

			PageList<NewsPo> list = (PageList<NewsPo>) newsService.query(queryFilter);
			APIPageList<NewsPo> pageList = getAPIPageList(list);
			result.setData(pageList);
			result.addVariable("pageResult", list.getPageResult());
			result.setMessage("成功获取公告列表");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取公告列表失败", e);
		}
		return result;
	}

	@ApiOperation(value = "公告信息", notes = "详细公告信息")
	@RequestMapping(value = "/getNews", method = RequestMethod.GET)
	public APIResult<NewsPo> getNews(
			@ApiParam(name = "id", value = "公告ID", required = true) @RequestParam(name = "id", required = true) String id) {
		APIResult<NewsPo> result = new APIResult<>();
		try {
			NewsPo po = newsService.get(id);
			result.setData(po);
			result.setMessage("成功获取公告信息");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取公告信息失败", e);
		}
		return result;
	}

	@ApiOperation(value = "删除公告", notes = "多个ID用,隔开")
	@RequestMapping(value = "/removeNews", method = RequestMethod.GET)
	public APIResult<String> removeNews(
			@ApiParam(name = "id", value = "公告ID", required = true) @RequestParam(name = "id", required = true) String id) {
		APIResult<String> result = new APIResult<>();
		if (BeanUtils.isEmpty(id)) {
			return getResult(result, FAIL, "参数为空");
		}
		try {
			newsService.remove(id.split(","));
			result.setMessage("成功删除公告");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "删除公告失败", e);
		}
		return result;
	}

	@ApiOperation(value = "保存公告", notes = "保存公告")
	@RequestMapping(value = "/saveNews", method = RequestMethod.POST)
	public APIResult<String> saveNews(
			@ApiParam(name = "newsJson", value = "新闻信息", required = true) @RequestParam(name = "newsJson", required = true) String newsJson) {
		APIResult<String> result = new APIResult<>();
		try {
			JSONObject obj = JSONObject.fromObject(newsJson);
			NewsPo po = (NewsPo) JSONObject.toBean(obj, NewsPo.class);
			newsService.save(po);
			String newsId = po.getId();
			String deptIdStr = po.getDepId();
			newsRightsService.saveNewsRights(deptIdStr, newsId);
			result.setMessage("保存公告成功");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "保存公告失败", e);
		}
		return result;
	}

}
